package com.situ.layuimini.controller;

import com.situ.layuimini.config.WxProperties;
import com.situ.layuimini.entity.User;
import com.situ.layuimini.service.IUserService;
import com.situ.layuimini.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "登录控制器")
@RestController
@RequestMapping(value = "/")
public class LoginController {

    @Autowired
    private IUserService userService;

    @Value("${token.secret}")
    private String secret;

    @Autowired
    private WxProperties wxProperties;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @ApiOperation(value = "检查用户名密码，做登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query", dataType = "String", required = true)
    })
    @PostMapping(value = "/login")
    public String login(String username, String password) {
        return userService.login(username, password);
    }

    //获取当前登录账号的所有信息
    @GetMapping(value = "/current")
    public User currentUser(HttpServletRequest request) {
        System.out.println(request.getSession().getId());
        String token = request.getHeader("Authorization");
        User u = TokenUtil.parse(token);
        return userService.getById(u.getId());
    }
}
